package com.example.cloudtrade;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class AddActivity3 extends AppCompatActivity {

    private RadioGroup switchRadioGroup;
    private RadioButton accountRadioButton;
    private RadioButton propRadioButton;

    private LinearLayout accountForm;
    private LinearLayout propForm;

    private EditText qq_account_edit_text;
    private EditText qq_password_edit_text;
    private Spinner QufuSpinner;
    private Spinner provinceSpinner;
    private EditText account_level_edit_text;
    private EditText goods_description_edit_text;
    private ImageView goods_description_image_view;
    private Button choose_image_button;
    private EditText price_edit_text;
    private EditText phone_edit_text;

    private EditText prop_name_edit_text;
    private EditText prop_quantity_edit_text;
    private EditText DJ_description_edit_text;
    private ImageView DJ_description_image_view;
    private Button choose_image_button2;
    private EditText DJ_price_edit_text;
    private EditText DJ_phone_edit_text;
    private Spinner serverSpinner_DJ_type;

    private Button submit_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add3);

        // 初始化视图组件
        initViews();

        // 设置 RadioGroup 切换监听
        switchRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.accountRadioButton) {
                    accountForm.setVisibility(View.VISIBLE);
                    propForm.setVisibility(View.GONE);
                } else if (checkedId == R.id.propRadioButton) {
                    accountForm.setVisibility(View.GONE);
                    propForm.setVisibility(View.VISIBLE);
                }
            }
        });

        // 模拟区服数据
        List<String> qufuList = new ArrayList<>();
        qufuList.add("1区");
        qufuList.add("2区");
        qufuList.add("3区");
        ArrayAdapter<String> qufuAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, qufuList);
        qufuAdapter.setDropDownViewResource(R.layout.custom_spinner_item);
        QufuSpinner.setAdapter(qufuAdapter);

        // 模拟段位数据（这里复用之前段位示例的逻辑，你可根据实际调整）
        List<String> rankList = new ArrayList<>();
        rankList.add("青铜");
        rankList.add("白银");
        rankList.add("黄金");
        ArrayAdapter<String> rankAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, rankList);
        rankAdapter.setDropDownViewResource(R.layout.custom_spinner_item);
        provinceSpinner.setAdapter(rankAdapter);


        // 模拟道具类型数据
        List<String> propTypeList = new ArrayList<>();
        propTypeList.add("金币");
        propTypeList.add("饰品");
        ArrayAdapter<String> propTypeAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, propTypeList);
        propTypeAdapter.setDropDownViewResource(R.layout.custom_spinner_item);
        serverSpinner_DJ_type.setAdapter(propTypeAdapter);

        // 设置提交按钮点击事件
        submit_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (accountRadioButton.isChecked()) {
                    // 验证账号表单数据
                    if (validateAccountForm()) {
                        // 这里可以添加实际的提交账号商品数据逻辑，比如网络请求等
                        Toast.makeText(AddActivity3.this, "账号商品信息提交成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(AddActivity3.this, MainActivity.class);
                        startActivity(intent);
                    }
                } else if (propRadioButton.isChecked()) {
                    // 验证道具表单数据
                    if (validatePropForm()) {
                        // 这里可以添加实际的提交道具商品数据逻辑，比如网络请求等
                        Toast.makeText(AddActivity3.this, "道具商品信息提交成功", Toast.LENGTH_SHORT).show();
//                        Intent intent = new Intent(AddActivity3.this, MainActivity.class);
//                        startActivity(intent);
                        finish();
                    }
                }
            }
        });
    }

    private void initViews() {
        switchRadioGroup = findViewById(R.id.switchRadioGroup);
        accountRadioButton = findViewById(R.id.accountRadioButton);
        propRadioButton = findViewById(R.id.propRadioButton);

        accountForm = findViewById(R.id.accountForm);
        propForm = findViewById(R.id.propForm);

        qq_account_edit_text = findViewById(R.id.qq_account_edit_text);
        qq_password_edit_text = findViewById(R.id.qq_password_edit_text);
        QufuSpinner = findViewById(R.id.QufuSpinner);
        provinceSpinner = findViewById(R.id.provinceSpinner);
        account_level_edit_text = findViewById(R.id.account_level_edit_text);
        goods_description_edit_text = findViewById(R.id.goods_description_edit_text);
        goods_description_image_view = findViewById(R.id.goods_description_image_view);
        choose_image_button = findViewById(R.id.choose_image_button);
        price_edit_text = findViewById(R.id.price_edit_text);
        phone_edit_text = findViewById(R.id.phone_edit_text);

        prop_name_edit_text = findViewById(R.id.prop_name_edit_text);
        prop_quantity_edit_text = findViewById(R.id.prop_quantity_edit_text);
        DJ_description_edit_text = findViewById(R.id.DJ_description_edit_text);
        DJ_description_image_view = findViewById(R.id.DJ_description_image_view);
        choose_image_button2 = findViewById(R.id.choose_image_button2);
        DJ_price_edit_text = findViewById(R.id.DJ_price_edit_text);
        DJ_phone_edit_text = findViewById(R.id.DJ_phone_edit_text);
        serverSpinner_DJ_type = findViewById(R.id.serverSpinner_DJ_type);

        submit_button = findViewById(R.id.submit_button);
    }

    private boolean validateAccountForm() {
        String qqAccount = qq_account_edit_text.getText().toString().trim();
        String qqPassword = qq_password_edit_text.getText().toString().trim();
        String accountLevel = account_level_edit_text.getText().toString().trim();
        String price = price_edit_text.getText().toString().trim();
        String phone = phone_edit_text.getText().toString().trim();

        if (qqAccount.isEmpty()) {
            Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (qqPassword.isEmpty()) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (accountLevel.isEmpty()) {
            Toast.makeText(this, "请输入账号等级", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (price.isEmpty()) {
            Toast.makeText(this, "请输入商品售价", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (phone.isEmpty()) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    private boolean validatePropForm() {
        String propName = prop_name_edit_text.getText().toString().trim();
        String propQuantity = prop_quantity_edit_text.getText().toString().trim();
        String price = DJ_price_edit_text.getText().toString().trim();
        String phone = DJ_phone_edit_text.getText().toString().trim();

        if (propName.isEmpty()) {
            Toast.makeText(this, "请输入道具名称", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (propQuantity.isEmpty()) {
            Toast.makeText(this, "请输入道具数量", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (price.isEmpty()) {
            Toast.makeText(this, "请输入商品售价", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (phone.isEmpty()) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }
    public void quxiao(View view) {
        // 处理取消按钮的点击事件
//        Intent intent = new Intent(AddActivity3.this, AddActivity2.class);
//        startActivity(intent);
        finish();
    }
}